package com.example.sea.core.data.user.service;

import com.example.sea.core.common.utils.UUIDUtil;
import com.example.sea.core.data.user.AuthPermissionEnum;
import com.example.sea.core.data.user.AuthRoleEnum;
import com.example.sea.core.data.user.entity.AuthRole;
import com.example.sea.core.data.user.entity.AuthUser;
import com.example.sea.core.data.user.mapper.AuthRoleMapper;
import com.example.sea.core.data.user.mapper.AuthUserMapper;
import com.example.sea.core.data.user.model.AuthPermissionModel;
import com.example.sea.core.data.user.model.AuthRolePermissionModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuthRoleService {

    @Autowired
    private AuthRoleMapper authRoleMapper;

    @Autowired
    private AuthPermissionService authPermissionService;

    @Autowired
    private AuthUserMapper authUserMapper;

    public PageInfo<AuthRole> findPageByParams(Map params){
        PageHelper.startPage(Integer.valueOf(params.get("pageNum").toString()),Integer.valueOf(params.get("pageSize").toString()));
        List<AuthRole> authRoles = authRoleMapper.selectListByParamsMap(params);
        PageInfo<AuthRole> pageInfo = new PageInfo<>(authRoles);
        return pageInfo;
    }

    @Transactional
    public int add(AuthRole authRole){
        authRole.setId(UUIDUtil.getUUID32());
        authRole.setCreateTime(new Date());
        authRole.setType(AuthRoleEnum.TYPE_BACK_PERMISSION.getValue());
        return authRoleMapper.insert(authRole);
    }

    @Transactional
    public int delete(String[] ids){
        int count=0;
        for (String id:ids) {
            //先判断是否有用户是该角色
            Map params=new HashMap();
            params.put("roleId",id);
            List<AuthUser> authUsers = authUserMapper.selectListByParamsMap(params);
            if(CollectionUtils.isEmpty(authUsers)){
                //先删除角色权限关联表的关联数据
                authRoleMapper.deleteRolePermissionByRoleId(id);
                count += authRoleMapper.deleteByPrimaryKey(id);
            }
        }
        return count;
    }

    /**
     * 根据ID进行查询
     * @param id
     * @return
     */
    public AuthRole findById(String id){
        return authRoleMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public int update(AuthRole authRole){
        return authRoleMapper.updateByPrimaryKey(authRole);
    }

    /**
     * 查询角色拥有的权限
     * @param roleId
     * @return
     */
    public List<AuthPermissionModel> findByIdToRolePermission(String roleId){
        List<AuthRolePermissionModel> authRolePermissionModels = authRoleMapper.selectListByRoleIdToRolePermission(roleId);
        List<AuthPermissionModel> tree = authPermissionService.findTree(AuthPermissionEnum.TYPE_BACK_PERMISSION.getValue());
        for (AuthPermissionModel authPermissionModel:tree) {
            setIsRoleState(authRolePermissionModels, authPermissionModel);
            if (authPermissionModel.getAuthPermissionModels().size()!=0){
                for (AuthPermissionModel authPermissionMenuModel:authPermissionModel.getAuthPermissionModels()) {
                    setIsRoleState(authRolePermissionModels, authPermissionMenuModel);
                }
            }

        }
        return tree;
    }

    @Transactional
    public void updateRolePermission(String[] ids,String roleId){
        authRoleMapper.deleteRolePermissionByRoleId(roleId);
        if (ids.length!=0){
            for (int i = 0; i < ids.length ; i++) {
                Map params=new HashMap();
                params.put("roleId",roleId);
                params.put("permissionId",ids[i]);
                authRoleMapper.insertRolePermission(params);
            }
        }
    }

    public List<AuthRole> findRoleByStateIsOkToAll(){
        Map params=new HashMap();
        params.put("state",AuthRoleEnum.IS_ROLE_Y.getValue());
        return authRoleMapper.selectListByParamsMap(params);
    }

    /**
     * 判断是否是角色选中菜单
     * @param authRolePermissionModels
     * @param authPermissionModel
     */
    private void setIsRoleState(List<AuthRolePermissionModel> authRolePermissionModels, AuthPermissionModel authPermissionModel) {
        for (AuthRolePermissionModel authRolePermissionModel:authRolePermissionModels) {
            if (authPermissionModel.getId().equals(authRolePermissionModel.getPermissionId())){
                authPermissionModel.setIsRole(AuthRoleEnum.IS_ROLE_Y.getValue());
            }
        }
    }
}
